<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :rules="validatorRules" :model="model" layout="vertical">
          <a-form-model-item
            label="表名" prop="formTableName">
            <a-input placeholder="请输入表名" v-model='model.formTableName'  @change="handleFormTableName" @blur="tableBlur" />
          </a-form-model-item>
          <a-form-model-item
            label="唯一编码" prop="relationCode" >
            <a-input placeholder="请输入唯一编码" v-model='model.relationCode'  :readOnly="relationCodeReadonly" />
          </a-form-model-item>
          <a-form-model-item
            label="流程状态列名" prop="flowStatusCol"  >
            <a-input placeholder="请输入流程状态字段" v-model='model.flowStatusCol'   />
          </a-form-model-item>
          <a-form-model-item
            label="标题字段" prop="titleExp"  >
            <a-select v-model="model.titleExp"  placeholder="请选择标题字段" >
               <a-select-option :value="item.fieldName" :key="index"  v-for="(item,index) in tableFieldList" >{{item.fieldRemark}}</a-select-option>
            </a-select>
          </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction,getAction } from '@/api/manage'

  export default {
    name: "ExtActProcessFormModal",
    data () {
      return {
        title:"操作",
        visible: false,
        relationCodeReadonly:true,
        model: {},
        processId: '',
        tableFieldList: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        formTypeValue:"1",
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          relationCode: [{ required: true, message: '请输入唯一编码!' }],
          formTableName: [{ required: true, message: '请输入表名!' }],
          flowStatusCol: [{ required: true, message: '请输入流程状态字段!' }],
          titleExp: [{ required: true, message: '请输入标题表达式!' }],
          processId: [{ required: true, message: '请输入流程ID!' }],
        },
        url: {
          add: "/process/extActProcessForm/add",
          edit: "/process/extActProcessForm/edit",
          queryByProcessId: "/process/extActProcessForm/queryByProcessId",
          getTableFieldList: "/process/extActProcessForm/getTableFieldList",
        },
      }
    },
    created () {
    },
    mounted(){
    },
    methods: {
      getTableFields(tableName){
         getAction(this.url.getTableFieldList,{tableName:tableName}).then(res => {
              if(res.success){
                this.tableFieldList = res.result;
              }else {
                this.$message.warn(res.message);
              }
         });
      },
      tableBlur(){
        if(this.model.formTableName){
          this.getTableFields(this.model.formTableName);
        }
      },
      async show(processId) {
        this.visible = true;
        this.tableFieldList = [];
        this.processId = processId;
        let  data = await this.getFormDetail(processId);
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          if(data.success) {
            this.model = Object.assign({},data.result);
            this.tableBlur();
          }else {
            this.model = Object.assign({}, {});
          }
        });
      },
     async getFormDetail(processId) {
       return  await getAction(this.url.queryByProcessId,{processId:processId});
      },
      handleFormTableName(e){
        var value = e.target.value;
        console.log(value);
        if(value!=null&&value!=''&&value!=undefined){
          var flowCode = "onl_"+value;
          this.model.relationCode = flowCode;
        }else{
          this.model.relationCode = "";
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate((ok, err) => {
          if (ok) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            //时间格式化
            if(this.processId){
              this.model.processId = this.processId;
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style scoped>

</style>